package com.example.skymallseckillproductapi.service;

import com.situ.model.ProductCategory;
import com.situ.utils.JsonResult;
import org.springframework.cloud.openfeign.FeignClient;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@FeignClient(value= "skymall-gateway",contextId = "categoryFeignService",fallback = CategoryFeignServiceFallback.class)
public interface CategoryFeignService {
    @GetMapping("/product-category-api/api/v1/productCategories/id/{id}")
    ResponseEntity<JsonResult<ProductCategory>> getById(@PathVariable Integer id);
}

//当通过Feign调用product-category服务失败时（如网络问题、服务宕机等），会触发降级机制
//这时候不会直接抛出异常，而是执行Fallback类中定义的备用逻辑
@Component
 class CategoryFeignServiceFallback implements CategoryFeignService{
    @Override
    public ResponseEntity <JsonResult<ProductCategory>> getById(Integer id) {
        return  ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).body(new JsonResult<>(503,false,"下游服务不可用",null));
    }
}
